//
//  CitySerchBar.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "CitySerchBar.h"
#import "CGFdbmBase.h"
#import "PinYinForObjc.h"
#import "ChineseInclude.h"
#import "CityModel.h"
#import "Masonry.h"
@interface CitySerchBar ()<UISearchBarDelegate>
@property (nonatomic,strong) NSMutableArray *allArea;
@property (nonatomic,strong) NSMutableArray *rerultAry;
@property (nonatomic,strong) NSMutableArray *CityAndAreaAry;
@property (nonatomic,strong) NSMutableArray *cityPinyAry;
@property (nonatomic,strong) NSMutableArray *cityPYHeadAry;

@end

@implementation CitySerchBar

-(NSMutableArray *)cityPYHeadAry{
    if (!_cityPYHeadAry) {
        _cityPYHeadAry = [NSMutableArray array];
        
        [_cityPYHeadAry addObjectsFromArray:[CGFdbmBase SeachWhithPath:@"diaoyur_city" AndTableName:@"region_city" IdStrName:@"py"]];

    }

    return _cityPYHeadAry;
}
-(NSMutableArray *)cityPinyAry{

    if (!_cityPinyAry) {
        
        _cityPinyAry = [NSMutableArray array];
      [_cityPinyAry addObjectsFromArray:[CGFdbmBase SeachWhithPath:@"diaoyur_city" AndTableName:@"region_city" IdStrName:@"pinyin"]];
        
    }
    return _cityPinyAry;
}

-(NSMutableArray *)rerultAry{
    if (!_rerultAry) {
        _rerultAry = [NSMutableArray array];
    }
    return _rerultAry;
}

-(NSMutableArray *)CityAndAreaAry{

    if (!_CityAndAreaAry) {
        _CityAndAreaAry = [NSMutableArray array];
        
        [_CityAndAreaAry addObjectsFromArray:[CGFdbmBase SeachWhithPath:@"diaoyur_city" AndTableName:@"region_city" IdStrName:@"name"]];
        
        [_CityAndAreaAry addObjectsFromArray:[CGFdbmBase SeachWhithPath:@"diaoyur_city" AndTableName:@"region_area" IdStrName:@"name"]];
    }
    return _CityAndAreaAry;
}

-(NSMutableArray *)allArea{
    if (!_allArea) {
        _allArea = [NSMutableArray array];
        
        [_allArea addObjectsFromArray:[CGFdbmBase SeachWhithPath:@"diaoyur_city" AndTableName:@"region_city" IdStrName:@"name"]];

    }
    return _allArea;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super  initWithFrame:frame]) {
        
        /**
         *  取消 self的点击手势
         */
        for (UIGestureRecognizer *tap in self.gestureRecognizers) {
            [self removeGestureRecognizer:tap];
        }
        
        [self backgroundSet];
        
        [self CancelBtnSet];
        
         [self setSearchTextFieldborderColor:[UIColor lightGrayColor]];

        self.delegate = self;

        self.placeholder = @"输入城市名称查询";
        
        [self BottomLineCreat];
        

     }
    return self;
}
-(void)BottomLineCreat{

   UIView *lin  = [UIView new];
    lin.backgroundColor  = [UIColor lightGrayColor];
    [self addSubview:lin];
    [lin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.height.equalTo(@0.7);
    }];

}

/**
 *   修改输入框
 */
- (void)setSearchTextFieldborderColor:(UIColor *)borderColor
{
   UIView *searchTextField = nil;
    if (iOS7) {
           // 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
           searchTextField = [[[self.subviews firstObject] subviews] lastObject];
       } else { // iOS6以下版本searchBar内部子视图的结构不一样
                for (UIView *subView in self.subviews) {
                       if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
                              searchTextField = subView;
                          }
                   }
           }
    
    
        searchTextField.layer.cornerRadius = 8.0f;
            searchTextField.layer.masksToBounds = YES;
        searchTextField.layer.borderColor = borderColor.CGColor;
            searchTextField.layer.borderWidth = 0.5f;

}

/**
 *  修改背景框
 */
-(void)backgroundSet{
    
    if ([self respondsToSelector:@selector(barTintColor)]) {
        if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.1) {
            //ios7.1
            [[[[self.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
            [self setBackgroundColor:[UIColor whiteColor]];
        }else{
            //ios7.0
            [self setBarTintColor:[UIColor whiteColor]];
            [self setBackgroundColor:[UIColor whiteColor]];
        }
    }else{
        //iOS7.0 以下
        [[self.subviews objectAtIndex:0] removeFromSuperview];
        [self setBackgroundColor:[UIColor whiteColor]];
    }

}

/**
 *  修改 取消按钮
 */
-(void)CancelBtnSet{
    
    self.showsCancelButton = YES;
    UIView *topView = self.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            _cancelButton = (UIButton*)subView;
        }
    }
    if (_cancelButton) {
        //Set the new title of the cancel button
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:mainColor forState:UIControlStateNormal];
    }
    
     _cancelButton.enabled = YES;
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    return YES;
}

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{

    return YES;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    [self.rerultAry removeAllObjects];

            if (searchText.length>0&&![ChineseInclude isIncludeChineseInString:searchText]) {
                
                for (int i=0; i<self.allArea.count; i++) {
                    
                    if ([ChineseInclude isIncludeChineseInString:self.allArea[i]]) {
                        if(searchText.length == 1){
                            
                            NSString *tempPinYinStr = self.cityPinyAry[i];
                            
                            NSRange titleResult=[tempPinYinStr rangeOfString:searchText options:NSCaseInsensitiveSearch];

                            if (titleResult.length>0) {

                                    [self.rerultAry addObject:self.allArea[i]];
                            }
                        }else{
                            
                           NSString *tempPinYinStr = self.cityPinyAry[i];
                            
                            NSRange titleResult=[tempPinYinStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
                            if (titleResult.length>0) {

                                    [self.rerultAry addObject:self.allArea[i]];

                            }
                            NSString *tempPinYinHeadStr = self.cityPYHeadAry[i];
                            
                            NSRange titleHeadResult=[tempPinYinHeadStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
                            if (titleHeadResult.length>0) {

                                    [self.rerultAry addObject:self.allArea[i]];
                                
                            }
                        }
                    }
                    else {
                        NSRange titleResult=[self.allArea[i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
                        if (titleResult.length>0) {

                                [self.rerultAry addObject:self.allArea[i]];
                        }
                    }
                }
            } else if (searchText.length>0&&[ChineseInclude isIncludeChineseInString:searchText]) {
                //中文搜索
                
                for (NSString *tempStr in self.CityAndAreaAry) {
                    NSRange titleResult=[tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
                    if (titleResult.length>0) {
                        
                            [self.rerultAry addObject:tempStr];
                    }
                }
                
            }
    
                  if (searchText.length == 0) {
                      
                      [self.Maideleget SearchReault:[self.rerultAry copy] AndIsAction:NO];
                      
                  }else{
                      
                    [self.Maideleget SearchReault:[self.rerultAry copy] AndIsAction:YES];
                      
                  }
}

/**
 *  取消按钮 代理事件
 */
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
   
    [self.Maideleget CancelClick];
}

@end
